//
//  HFStatusOriginalView.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/17.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class HFStatusOriginalView: UIView {
    
    //定义约束条件
    var bottomConstrains: Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    var status: HFStatus? {
        didSet {
            iconImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberShipIconView.image = status?.user?.mbrankImage
            verifiedIconView.image = status?.user?.verified_type_Image
            timeLabel.text = "11:11"//status?.created_at
            sourceLabel.text =  "村头"//status?.source
            contentLabel.text = status?.text
            
            //移除原有约束
            self.bottomConstrains?.uninstall()
            //判断是否有配图并更新约束
            if let pictureurls = status?.imageURLs where pictureurls.count > 0 {
                pictureView.imageURLs = status?.imageURLs
                //显示图片
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrains = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint

                })
            }else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrains = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        //添加控件
        sepView.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(memberShipIconView)
        addSubview(verifiedIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)

        //添加约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }

        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.height.equalTo(StatusCellIconWitdth)
            make.width.equalTo(StatusCellIconWitdth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        memberShipIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.width.equalTo(100)
            make.height.equalTo(100)
        }


        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstrains = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

    }
    //懒加载控件
    //头像图片
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户昵称
    private lazy var nameLabel: UILabel = UILabel(title: "醉红颜", size: 17, color: UIColor.lightGrayColor())
    //用户认证类型
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var memberShipIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor())
    //微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "扭曲丛林", size: 10, color: UIColor.lightGrayColor())
    //微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "彩蛋:看见长得漂亮的植物会兴奋", size: 14, color: UIColor.lightGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView: HFStatusPictureView = HFStatusPictureView()
    private lazy var sepView: UIView = UIView()
    
}
